package com.tanovo.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tanovo.bo.DepartmentUserBo;
import com.tanovo.bo.DepartmentUserQuery;
import com.tanovo.bo.ResultObject;
import com.tanovo.config.UserInfoContext;
import com.tanovo.domain.Department;
import com.tanovo.domain.User;
import com.tanovo.domain.UserBaseInfo;
import com.tanovo.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "部门管理")
@RestController
public class DepartmentApi {

    @Autowired
    private IDepartmentService iDepartmentService;

    @PostMapping("/department/add")
    @ApiOperation(value = "添加部门", notes = "添加部门")
    public ResultObject addDepartment(@RequestBody Department department) {
        department.setClientId(UserInfoContext.getUser().getClientId());
        department.setCompanyId(UserInfoContext.getUser().getCompanyId());
        department.setUpdateBy(UserInfoContext.getUser().getId().toString());
        department.setCreateBy(UserInfoContext.getUser().getId().toString());
        iDepartmentService.addDepartment(department);
        return new ResultObject();
    }

    @PostMapping("/department/delete")
    @ApiOperation(value = "删除部门list", notes = "删除部门list")
    public ResultObject delete(@RequestBody List<Integer> departmentIdList) {
        iDepartmentService.delDepartment(departmentIdList);
        return new ResultObject();
    }

    @PostMapping("/department/update")
    @ApiOperation(value = "修改编辑部门", notes = "修改编辑部门")
    public ResultObject editDepartment(@RequestBody Department department) {
        department.setUpdateBy(UserInfoContext.getUser().getId().toString());
        iDepartmentService.editDepartment(department);
        return new ResultObject();
    }

    @GetMapping("/department/listbyParentId")
    @ApiOperation(value = "通过父部门查找子部门", notes = "通过父部门查找子部门 顶级部门的父id=0")
    public ResultObject listbyParentId(@RequestParam Integer parentId) {
        List<Department> list = iDepartmentService.listbyParentId(parentId);
        return new ResultObject(list);
    }

    @PostMapping("/department/not/userList")
    @ApiOperation(value = "部门没有添加的员工列表", notes = "部门没有添加的员工列表")
    public ResultObject<List<UserBaseInfo>> userNotList(@RequestBody DepartmentUserQuery departmentUserQuery) {
        PageHelper.startPage(departmentUserQuery.getPageNum(),departmentUserQuery.getPageSize());
        departmentUserQuery.setCompanyId(UserInfoContext.getUser().getCompanyId());
        departmentUserQuery.setClientId(UserInfoContext.getUser().getClientId());
        List<UserBaseInfo> list = iDepartmentService.userNotList(departmentUserQuery);
        PageInfo pageInfo=new PageInfo(list);
        return new ResultObject(pageInfo);
    }

    @PostMapping("/department/userList")
    @ApiOperation(value = "查找部门添加用户列表", notes = "查找部门添加用户列表")
    public ResultObject<List<UserBaseInfo>> userList(@RequestBody DepartmentUserQuery departmentUserQuery) {
        PageHelper.startPage(departmentUserQuery.getPageNum(),departmentUserQuery.getPageSize());
        departmentUserQuery.setCompanyId(UserInfoContext.getUser().getCompanyId());
        departmentUserQuery.setClientId(UserInfoContext.getUser().getClientId());
        List<UserBaseInfo> list = iDepartmentService.userList(departmentUserQuery);
        PageInfo pageInfo=new PageInfo(list);
        return new ResultObject(pageInfo);
    }

    @PostMapping("/department/bindUser")
    @ApiOperation(value = "部门编辑员工", notes = "部门编辑员工")
    public ResultObject bindUser(@RequestBody DepartmentUserBo departmentUserBo) {
        departmentUserBo.setUpdateBy(UserInfoContext.getUser().getId().toString());
        departmentUserBo.setCreateBy(UserInfoContext.getUser().getId().toString());
        iDepartmentService.bindDepartmentUser(departmentUserBo);
        return new ResultObject();
    }

}
